use sdl2::pixels::Color;
use sdl2::render::TextureQuery;
use sdl2::rect::Rect;
use sdl2::ttf::Font;
use crate::app::*;
use crate::tetris::*;
use crate::gfx::*;
use crate::constant::*;


pub struct GameView;

impl GameView {
    pub fn new(game: &mut App) -> GameView {
        game.tetris.start_game();
        GameView{}
    }
}

impl View for GameView {
    fn render(&mut self, game: &mut App, elapsed: f64) -> ViewAction {
        if game.events.quit || game.events.key_escape {
            return ViewAction::Quit
        }
        if game.events.now.key_left == Some(true) {
            let next_position = game.tetris.col - 1;
            game.tetris.set_col(next_position);
        }

        if game.events.now.key_right == Some(true) {
            let next_position = game.tetris.col + 1;
            game.tetris.set_col(next_position);
        }

        if game.events.now.key_down == Some(true) {
            let next_position = game.tetris.row + 1;
            game.tetris.set_row(next_position);
        }

        if game.events.now.key_up == Some(true) {
            game.tetris.rotate();
        }

        if game.events.now.key_p == Some(true) {
            game.tetris.set_pause();
        }

        if game.events.now.key_space == Some(true) {
            game.tetris.drop_downtest();
        }

        if game.tetris.game_over {
            if game.events.now.key_r == Some(true) {
                game.tetris.start_game();
            }
        }

        game.elapsed += elapsed;
        if game.elapsed > 1f64/(game.tetris.level + 1) as f64 {
            game.tetris.tick_down();
            game.elapsed = 0f64;
        }

        game.canvas.set_draw_color(BLACK);
        game.canvas.clear();

        game.canvas.set_draw_color(LIGHT_GRAY);
        //画主面板
        game.canvas.fill_rect(Rect::new(LEFT_MARGIN, TOP_MARGIN, BOARD_WIDTH, BOARD_HEIGHT));
        //画预览面板
        game.canvas.fill_rect(Rect::new(LEFT_MARGIN + (BOARD_WIDTH + BOARD_MARGIN_RIGHT) as i32,
                                          TOP_MARGIN,
                                          PREVIEW_BOARD_WIDTH,
                                          PREVIEW_BOARD_WIDTH));

        //画面板内的方格
        for col in 0..COL_COUNT {
            for row in 0..ROW_COUNT {
                if game.tetris.grid[col as usize][row as usize].cell_type == GridCellType::Shape|| game.tetris.grid[col as usize][row as usize].cell_type == GridCellType::Fixed {
                    game.canvas.set_draw_color(get_shape_color(game.tetris.grid[col as usize][row as usize].shape_index));
                    game.canvas.fill_rect(Rect::new(LEFT_MARGIN + col as i32 * CELL_SIZE,
                                                      TOP_MARGIN + row as i32 * CELL_SIZE,
                                                      CELL_SIZE as u32 - 1,
                                                      CELL_SIZE as u32 - 1
                    ));
                }
            }
        }
        //画预览面板内方格
        if !game.tetris.game_over {
            let (width, height, minx, miny) = game.tetris.get_rect_min(game.tetris.next_shape);
            let x_left_margin = (PREVIEW_BOARD_COUNT as i32 - width) as f32/2.0_f32 * CELL_SIZE as f32;
            let y_up_margin = (PREVIEW_BOARD_COUNT as i32 - height) as f32/2_f32 * CELL_SIZE as f32;
            let offset_x = 0i32 - minx;
            let offset_y = 0i32 - miny;
            for point in game.tetris.next_shape.iter() {
                game.canvas.set_draw_color(get_shape_color(game.tetris.next_shape_index));
                game.canvas.fill_rect(Rect::new(LEFT_MARGIN as i32 + BOARD_WIDTH as i32 + BOARD_MARGIN_RIGHT as i32 + x_left_margin as i32 + (point.x as i32 + offset_x) * CELL_SIZE,
                                                  TOP_MARGIN + y_up_margin as i32 + (point.y as i32 + offset_y) * CELL_SIZE,
                                                  CELL_SIZE as u32 - 1,
                                                  CELL_SIZE as u32 - 1
                ));
            }
        }
        let creator = game.canvas.texture_creator();
        if game.tetris.game_over {
            //画游戏结束后显示的框
            {
                let op_over_sprite = game.ttf_str_sprite(&creator, "你挂了", 42u16, RED);
                let op_restart_sprite = game.ttf_str_sprite(&creator, "重新开始(R)", 42u16, WHITE);

                if let Some(over_sprite) = op_over_sprite {
                    let clone = over_sprite.tex.clone();
                    let TextureQuery { width: ref w1, height, .. } = clone.borrow().query();
                    let x = (WIDTH - w1) / 2;
                    let y = (HEIGHT - height) / 2;
                    over_sprite.render(&mut game.canvas, Rectangle::new(x as f64, (y - 100) as f64, *w1 as f64, height as f64));
                }

                if let Some(restart_sprite) = op_restart_sprite {
                    let clone = restart_sprite.tex.clone();
                    let TextureQuery { width, height, .. } = clone.borrow().query();
                    let x = (WIDTH - width) / 2;
                    let y = (HEIGHT - height) / 2;
                    restart_sprite.render(&mut game.canvas, Rectangle::new(x as f64, y as f64, width as f64, height as f64));
                }
            }
        }
        //渲染分数
        {
            let op_score_title_sprite = game.ttf_str_sprite(&creator, "score", 20u16, WHITE);
            if let Some(score_title_sprite) = op_score_title_sprite {
                let clone = score_title_sprite.tex.clone();
                let TextureQuery { width, height, .. } = clone.borrow().query();
                let x = LEFT_MARGIN as i32 + BOARD_WIDTH as i32 + BOARD_MARGIN_RIGHT as i32 + ((PREVIEW_BOARD_WIDTH - width)/2) as i32;
                let y = TOP_MARGIN + PREVIEW_BOARD_WIDTH as i32 + 10;
                score_title_sprite.render(&mut game.canvas, Rectangle::new(x as f64,y as f64, width as f64, height as f64));
            }


            let op_score_sprite = game.ttf_str_sprite(&creator, &format!("{}", game.tetris.score), 20u16, WHITE);
            if let Some(score_sprite) = op_score_sprite {
                let clone = score_sprite.tex.clone();
                let TextureQuery { width, height, .. } = clone.borrow().query();
                let score_x = LEFT_MARGIN as i32 + BOARD_WIDTH as i32 + BOARD_MARGIN_RIGHT as i32 + ((PREVIEW_BOARD_WIDTH - width)/2) as i32;
                let score_y = TOP_MARGIN + PREVIEW_BOARD_WIDTH as i32 + 10 + height as i32;
                score_sprite.render(&mut game.canvas, Rectangle::new(score_x as f64,score_y as f64, width as f64, height as f64));
            }
        }
        ViewAction::None
    }
}